﻿namespace FallingRocks
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Windows.Forms;         

    public static class ControlPanel   // completely static class, can be used from everywhere
    {
        // Import the last Settings
        public static void ReloadSettings()
        {
            ReloadFrom("../../settings.ini");
        }

        // Set the Default Settings
        public static void ReloadDefaultSettings()
        {
            ReloadFrom("../../defaultSettings.ini");
        }

        public static void ReloadFrom(string file)
        {
            string[] lines = ReadFrom(file);

            string[] command = 
            {
                "MOVING_LEFT",
                "MOVING_RIGHT",
                "ACTION",
                "PAUSE"
            };

            foreach (string line in lines)
            {
                var controls = Regex.Match(line, "(.*?) = (.*)").Groups;

                for (int i = 0; i < command.Length; i++)
                {
                    if (controls[1].Value == command[i])
                    {
                        Keys exportKey = (Keys)Enum.Parse(typeof(Keys), controls[2].ToString().Trim());
                        switch (i)
                        {
                            case 0:
                                Key.MovingLeft = (ConsoleKey)exportKey;
                                break;
                            case 1:
                                Key.MovingRight = (ConsoleKey)exportKey;
                                break;
                            case 2:
                                Key.Activate = (ConsoleKey)exportKey;
                                break;
                            case 3:
                                Key.Pause = (ConsoleKey)exportKey;
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
        }

        public static void SetNewSettings() // TODO Setting new controls from the user
        {
            /*
            Press Key For movingLeft
            Key.MovingLeft = Console.ReadKey().Key; 
            
            Press Key For movingRight
            Key.MovingRight = keys;
            
            Press Key For Activate
            Key.Activate = keys;
            
            Press Key For Pause
            Key.Pause = keys;
            
            ExportSettings();
            */
        }

        // export the new Settings
        public static void ExportSettings()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("[SETTINGS]\n");
            sb.Append("MOVING_LEFT = " + Key.MovingLeft + '\n');
            sb.Append("MOVING_RIGHT = " + Key.MovingRight + '\n');
            sb.Append("ACTION = " + Key.Activate + '\n');
            sb.Append("PAUSE = " + Key.Pause + '\n');                  // TODO can add background color or something else
            WriteToSettings_ini(sb.ToString().Split('\n'));
        }

        public static void WriteToSettings_ini(string[] lines)
        {
            using (StreamWriter output = new StreamWriter("../../settings.ini"))
            {
                foreach (string line in lines) 
                { 
                    output.WriteLine(line); 
                }
            }
        }

        private static string[] ReadFrom(string file)
        {
            using (StreamReader input = new StreamReader("../../settings.ini"))
            {
                List<string> list = new List<string>();

                for (string line; (line = input.ReadLine()) != null;)
                {
                    if (line.Contains('='))
                    {
                        list.Add(line);
                    }
                }

                string[] lines = new string[list.Count];

                for (int i = 0; i < lines.Length; i++)
                {
                    lines[i] = list[i].ToString();
                }

                return lines;
            }
        }
    }
}
